﻿using Org.Apache.Rocketmq;
using rocketmq.test;
using System.Text;

namespace SimpleConsumer2
{
    public class SimpleConsumerExample
    {
        public static async Task QuickStart()
        {
            var clientConfig = RocketMQHelper.GetRocketMQClientConfig();

            const string consumerGroup = "test_group_event";
            const string topic = "test_shipment";
            var subscription = RocketMQHelper.GetSubscription(topic);
            var simpleConsumer = await new SimpleConsumer.Builder()
                .SetClientConfig(clientConfig)
                .SetConsumerGroup(consumerGroup)
                .SetAwaitDuration(TimeSpan.FromSeconds(15))
                .SetSubscriptionExpression(subscription)
                .Build();

            while (true)
            {
                var messageViews = await simpleConsumer.Receive(16, TimeSpan.FromSeconds(15));
                foreach (var message in messageViews)
                {
                    var strMsg = Encoding.UTF8.GetString(message.Body);
                    Console.WriteLine($"Consumer2;topic:[{topic}]; consumerGroup:[{consumerGroup}], messageId:[{message.MessageId}]; message:[{strMsg}]");
                    await simpleConsumer.Ack(message);
                }
            }
            // Close the simple consumer if you don't need it anymore.
            //await simpleConsumer.DisposeAsync();
        }
    }
}
